package controller;

import java.util.Date;
import java.text.SimpleDateFormat;

import com.opensymphony.xwork2.ActionSupport;

import dao.AddTourDAO;
import entity.Tour;

public class AddTourAction extends ActionSupport{
		private SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy");
        private String tourName;
        private String place;
        private String startDate;
        private String endDate; 
        private float price;
        private String images;
        private short maxVisitors;
        private short currentVisitors;
        private String tag;
        
//        public void validate(){
//        	if(tourName == null || tourName.trim().equals("")){
//        		addFieldError("tourName", "This field is required");
//        	}
//        	if(tourName.length() > 50){
//        		addFieldError("tourName", "Not more than 50 characters");
//        	}
//        	if(place == null || place.trim().equals("")){
//        		addFieldError("place", "This field is required");
//        	}
//        	if(place.length() > 100){
//        		addFieldError("place", "Not more than 100 characters");
//        	}
//        	if(price < 0){
//        		addFieldError("price", "Can not be smaller than 0");
//        	}
//        	if(maxVisitors < 0){
//        		addFieldError("maxVisitors", "Can not be smaller than 0");
//        	}
//        	if(currentVisitors < 0){
//        		addFieldError("currentVisitors", "Must be in between 0 and " + maxVisitors);
//        	}
//        	if(currentVisitors > maxVisitors){
//        		addFieldError("currentVisitors", "Can not greater than max visitors");
//        	}
//        }
        
        @Override
        public String execute() throws Exception{
            Tour tour = new Tour();
            Date sDate, eDate;
            
            try {
            	sDate = sdf.parse(startDate);
            	eDate = sdf.parse(endDate);
            	if (sDate.getTime() > eDate.getTime()) {
            		addActionError(" sdate must < edate ");
            		throw new Exception(" startDate must < endDate");
            	}
            	tour.setTourName(tourName);
            	tour.setPlace(place);
                tour.setStartDate(new java.sql.Date(sDate.getTime()));
                tour.setEndDate(new java.sql.Date(eDate.getTime()));
                tour.setPrice(price);
                tour.setImages(images);
                tour.setMaxVisitors(maxVisitors);
                tour.setCurrentVisitors(currentVisitors);
                tour.setTag(tag);
	            
	            if (!AddTourDAO.Addtour(tour)) {
	            	System.out.println("add tour action says: didn't add new tour");
	            	return ERROR;
	            }	            
			} catch (Exception ex) {
				System.out.println(" add tour action say error:  " + ex.getMessage());
				return ERROR;
			}
            return SUCCESS;
        }            
                
		public String getTourName() {
			return tourName;
		}
		public void setTourName(String tourName) {
			this.tourName = tourName;
		}
		public String getPlace() {
			return place;
		}
		public void setPlace(String place) {
			this.place = place;
		}
		public String getStartDate() {
			return startDate;
		}
		public void setStartDate(String startDate) {
			this.startDate = startDate;
		}
		public String getEndDate() {
			return endDate;
		}
		public void setEndDate(String endDate) {
			this.endDate = endDate;
		}
		public float getPrice() {
			return price;
		}
		public void setPrice(float price) {
			this.price = price;
		}
		public String getImages() {
			return images;
		}
		public void setImages(String images) {
			this.images = images;
		}
		public short getMaxVisitors() {
			return maxVisitors;
		}
		public void setMaxVisitors(short maxVisitors) {
			this.maxVisitors = maxVisitors;
		}
		public short getCurrentVisitors() {
			return currentVisitors;
		}
		public void setCurrentVisitors(short currentVisitors) {
			this.currentVisitors = currentVisitors;
		}
		public String getTag() {
			return tag;
		}
		public void setTag(String tag) {
			this.tag = tag;
		}
}